<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/axios/1.7.2/axios.min.js"></script>
    <title>Axios</title>
</head>
<body>
    <button>GET</button>&nbsp;&nbsp;
    <button>POST</button>&nbsp;&nbsp;
    <button>AJAX</button>


    <script>
        let btns = document.getElementsByTagName('button')

        //配置baseUrl
        axios.defaults.baseURL = 'http://localhost:8000'


        btns[0].onclick = ()=>{

            //GET请求
            axios.get("/axios-server",{
                //url参数
                params:{
                    a:100,
                    vip:7
                }
                //请求头信息
                /**headers:{
                    name:"jack",
                    age:18
                }*/
            }).then(value=>{
                //axios.get()方法返回的是一个 Promise对象，使用回调函数进行处理返回数据
                console.log(value)
            },reason=>{
                console.log(reason)
            })  
        }
        
        btns[1].onclick = function(){
            axios.post("/axios-post",{
                name: "jack",
                age: 29
            },{
                //url
                params:{
                    a:100,
                    b:'jack'
                }
            }).then(value=>{
                console.log(value)
            })
        }


        //Axios的通用方式
        btns[2].onclick = function(){
            axios({
                //url
                url:'/axios-post',
                //请求方式
                method: "POST",
                //url参数
                params:{
                    a:100,
                    b:1000
                },
                //头信息参数
                headers:{
                    name:"jack",
                    school:"jxlgdx"
                },
                //请求体
                data:{
                    username:'admin',
                    password:"abc123"
                }

            }).then(value=>{
                console.log(value)
            })
        }

    </script>
    
</body>
</html>